package football.haha.com.footballtv.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import football.haha.com.footballtv.FootballApplication;
import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanLiveHall;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.ImageToos;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.view.BorderView;

/**
 * Created by haha on 2017/11/2.
 */

public class LiveHallActivity extends BaseActivity {

    @Bind(R.id.fl_live_content)
    FrameLayout flLiveContent;
    private int pageIndex = 1;
    private String folderCode = "";
    private BorderView border;

    public ArrayList<View> liveVideoItemList = new ArrayList<>();
    private BeanLiveHall beanLiveHall = null;
    private List<BeanLiveHall.ContentsBean> contentsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_hall);
        ButterKnife.bind(this);

        folderCode = getIntent().getStringExtra("folderCode");
        createOnlyBorderView(flLiveContent);



        getData();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            if (liveVideoItemList.contains(v) && liveVideoItemList.indexOf(v)>(liveVideoItemList.size()-3) && pageIndex<beanLiveHall.getTotalPages()){
                pageIndex ++;
                getData();
            }
        }
        super.onFocusChange(v, hasFocus);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        BeanLiveHall.ContentsBean contentsBean = contentsBeanList.get(liveVideoItemList.indexOf(view));

        getAuthPlayUrl(contentsBean.getName(), folderCode, contentsBean.getContentId(), contentsBean.getContentType(), contentsBean.getEndTime());
    }

    private void getData() {
        Map<String, String> params = new HashMap<>();
        params.put("command", "folderContents");
        params.put("busCode", "IPS");
        params.put("folderCode", folderCode);
        params.put("pageIndex", pageIndex + "");
        params.put("pageItems", "20");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGGetLiveHallData, params, BeanLiveHall.class, "记载中...", this);
    }

    @Override
    public void netBack(int requestTag, Object object) {
        super.netBack(requestTag, object);
        if (requestTag == ApiUtils.TAGGetLiveHallData) {
            beanLiveHall = (BeanLiveHall) object;
            if (pageIndex == 1) {
                contentsBeanList.clear();
            }
            contentsBeanList.addAll(beanLiveHall.getContents());
            addLiveVideo();
        }
    }

    public void addLiveVideo() {
        int allWidth = 0;
        int itemStartCount = liveVideoItemList.size();
        for (int i = 0; i < contentsBeanList.size(); i++) {
            View itemLive = View.inflate(this, R.layout.item_video, null);
            itemLive.setOnClickListener(this);
            itemLive.setOnFocusChangeListener(this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int) getResources().getDimension(R.dimen.px500),
                    (int) getResources().getDimension(R.dimen.px360));


            int topMargin = (int) getResources().getDimension(R.dimen.px200) +
                    (i + itemStartCount) % 2 * (int) getResources().getDimension(R.dimen.px370);
            int leftMargin = (int) getResources().getDimension(R.dimen.px95) +
                    (i + itemStartCount) / 2 * (int) getResources().getDimension(R.dimen.px510);

            params.topMargin = topMargin;
            params.leftMargin = leftMargin;
            params.rightMargin = (int) getResources().getDimension(R.dimen.px95);

            flLiveContent.addView(itemLive, params);
            allWidth = leftMargin + params.width + (int) getResources().getDimension(R.dimen.px95);
            liveVideoItemList.add(itemLive);

            if (i % 2 == 0) {
                itemLive.setNextFocusUpId(R.id.cb_look_back);
            }

            ImageView ivVideo = (ImageView) itemLive.findViewById(R.id.iv_video);
            TextView tvTitle = (TextView) itemLive.findViewById(R.id.tv_title);
            BeanLiveHall.ContentsBean contentsBean = contentsBeanList.get(i);

            List<BeanLiveHall.ContentsBean.ImagesBean> imagesBeanList = contentsBean.getImages();
            if (imagesBeanList!=null && imagesBeanList.size()>0){
                ImageToos.setImageWithUrl(imagesBeanList.get(0).getUrl(), ivVideo, FootballApplication.getOptions(R.drawable.default_video));
            }
            tvTitle.setText(contentsBean.getName());
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(allWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        flLiveContent.setLayoutParams(params);

        if (liveVideoItemList.size()<20 && liveVideoItemList.size()>0){
            showViewByHandler(liveVideoItemList.get(0));
        }

        border = new BorderView(this);
        border.setBackgroundResource(R.drawable.focus_rec_home);
        border.attachTo(flLiveContent);

    }

}
